package user.zc.excel.data.wrap;

import user.zc.excel.WriteDTO;
import user.zc.excel.data.info.ICell;
import lombok.SneakyThrows;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;
import java.util.Objects;

public class ColumnMergedRegionWrapper extends AWrapper{

    public ColumnMergedRegionWrapper(AWrapper next) {
        super(next);
    }

    @Override
    @SneakyThrows
    public WriteDTO merge(WriteDTO writeDTO) {

        String lastMergedName = null;
        ICell.CellInfo[] mergeColumn = new ICell.CellInfo[writeDTO.getColumns().length];
        int start = writeDTO.getTitles().length;
        for (int i = 0; i < writeDTO.getColumns().length; i++) {
            ICell.CellInfo column = writeDTO.getColumns()[i];
            mergeColumn[i] = column.clone();
            if(!"".equals(column.getMergeRelationColumn())){
                mergeColumn[i].setData(column.getMergeRelationColumn());
                lastMergedName = column.getMergeRelationColumn();
                int j = i;
                while(++i<writeDTO.getColumns().length){
                    column = writeDTO.getColumns()[i];
                    mergeColumn[i] = column.clone();
                    if(!lastMergedName.equals(column.getMergeRelationColumn())
                    ||(i==writeDTO.getColumns().length-1)){
                        i--;
                        mergeColumn[i].setCra(new CellRangeAddress(start,start,j,i));
                        lastMergedName = column.getMergeRelationColumn();
                        break;
                    }
                }
            }else{
                mergeColumn[i].setCra(new CellRangeAddress(start,start+1,i,i));
            }
        }
        if(lastMergedName!=null){
            writeDTO.setMerged(mergeColumn);
        }

        start += writeDTO.getMerged()!=null?2:1;
        
        //判断data里面是否需要上下合并的
        ICell.CellInfo[] cellInfos =   writeDTO.getColumns();
        List<ICell.CellInfo[]> cells = writeDTO.getCells();
        for (int i = 0; i < cellInfos.length; i++) {
            ICell.CellInfo column = cellInfos[i];
            int lastJ = 0;
            Object lastObject = null ;
            if (column.isRowSpanIfSameValue()) {
                for(int j=0;j<writeDTO.getDatas().size();j++){
                    Object value = getValue(writeDTO.getCells(),j,i);
                    if(value!=null && !value.equals(lastObject)){
                        if(j!=0){
                            cells.get(j-1)[i].setCra(new CellRangeAddress(start+lastJ,start+j-1,i,i));
                        }
                        lastJ = j;
                        lastObject = value;
                    }
                    //如果到了最后一行，并且有合并的，则合并
                    if(j==writeDTO.getDatas().size()-1 && j != lastJ ){
                        cells.get(j)[i].setCra(new CellRangeAddress(start+lastJ,start+j,i,i));
                    }
                }
            }
        }
        return writeDTO;
    }

    private Object getValue(List<ICell.CellInfo[]> cellInfoList, int i,int j) {
        return cellInfoList.get(i)[j].getData();
    }
}
